package com.iwjw.ibatis.generate.model;

import com.iwjw.ibatis.generate.util.BuildMapperUtil;
import com.iwjw.ibatis.generate.util.BuildModelUtil;
import com.iwjw.ibatis.generate.util.BuildXmlUtil;
import com.iwjw.ibatis.util.CamelCaseUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

/**
 * @author: huangweiwei
 * @email: ihuangweiwei@163.com
 * @created:2016/10/26
 */
@Data
@AllArgsConstructor
public class BuildRunnable implements Runnable {

    public static Logger logger = LoggerFactory.getLogger(BuildRunnable.class);

    private String tableName;

    private Connection connection;

    private CustomizeTableInfo customizeTableInfo;


    public void run() {
        //TODO 生成Mapper
        String modelName = CamelCaseUtils.toCapitalizeCamelCase(tableName);
        if (GenerateConfig.MAPPER_BUILD) {
            if (customizeTableInfo != null && !StringUtils.isEmpty(customizeTableInfo.getTableProperty())) {
                modelName = CamelCaseUtils.toCapitalizeCamelCase(customizeTableInfo.getTableProperty());
            }
            BuildMapperUtil.build(GenerateConfig.MAPPER_PKG, GenerateConfig.MODEL_PKG, modelName);
        }

        //TODO 生成model
        Boolean isBuildModel = BuildModelUtil.build(GenerateConfig.MODEL_PKG, tableName, customizeTableInfo, connection);
        logger.info("build table:{} is:{}", tableName, isBuildModel);
        //TODO 生成XML
        if (GenerateConfig.XML_BUILD) {
            BuildXmlUtil.build(GenerateConfig.XML_PATH, GenerateConfig.MAPPER_PKG, GenerateConfig.MODEL_PKG, modelName);

        }


    }
}
